import {
  createRouter,
  createWebHashHistory
} from 'vue-router'

import store from '../store'

const routes = [
  {
    path: '/',
    redirect: '/dashboard/analysis'
  },
  {
    path: '/login',
    component: () => require.ensure([], (require) => require(`../pages/login/index.vue`)),
  },//分裂管理
  {
    path: '/dashboard',
    name: 'dashboard',
    component: () => require.ensure([], (require) => require(`../pages/dashboard/dashboard.vue`)),
    children: [
      {
        path: '/dashboard/analysis',
        component: () => require.ensure([], (require) => require(`../pages/analysis/analysis.vue`)),
      },//分裂管理
      {
        path: '/dashboard/goods',
        component: () => require.ensure([], (require) => require(`../pages/goods/goods.vue`)),
      },//商品管理
      {
        path: '/addGoods',
        component: () => require.ensure([], (require) => require(`../pages/addGoods/addGoods.vue`)),
      },//上传商品
      {
        path: '/order/list',
        component: () => require.ensure([], (require) => require(`../pages/order/list.vue`)),
      },//订单列表
      {
        path: '/shop/sales',
        component: () => require.ensure([], (require) => require(`../pages/shop/sales.vue`)),
      },//订单列表
      {
        path: 'classify',
        component: () => require.ensure([], (require) => require(`../pages/classify/classify.vue`)),
      },
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next()
  } else {
    let { sea_amoy_admin_token } = store.state
    if (sea_amoy_admin_token === null || sea_amoy_admin_token === "") {
      next("/login")
    } else {
      next()
    }
  }
})

router.afterEach(() => {
  window.scrollTo(0, 0)
})

export default router
